//
//  WBHomeViewController.swift
//  weibo
//
//  Created by iobit on 2017/05/18.
//  Copyright © 2017 zhuxu. All rights reserved.
//

import UIKit

//全局常量，使用private，否者各处都可以访问
private let cellID = "cellID"

class WBHomeViewController: WBBaseViewController {

    // 列表视图模型
    lazy var listViewModel = WBStatusListViewModel()
    
    override func loadData() {
        
        //用网络工具加载微博数据
        print("开始加载数据")
        listViewModel.loadStatus(pullup: self.isPullUp ) { (isSuccess, shouldRefresh) in

            print("加载数据结束")
            //结束刷新控件
            self.refreshControl?.endRefreshing()
            //恢复上拉刷新标记
            self.isPullUp = false
            //刷新表格
            if shouldRefresh {
                self.tableView?.reloadData()               
            }

        }


    }
    
    
    /// 显示好友
    @objc func showFriends() {
        let vc = WBDemoViewController()
        
        //vc.hidesBottomBarWhenPushed = true//在push时隐藏下方导航栏
        navigationController?.pushViewController(vc, animated: true)
    }
}

// 表格数据源方法，具体数据实现
extension WBHomeViewController {
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return listViewModel.statusList.count
    }
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: cellID, for: indexPath) as! WBStatusCell
        // 设置cell
        let listModel = listViewModel.statusList[indexPath.row]

        cell.viewModel = listModel
        
        return cell
    }
}


extension WBHomeViewController {
    
    override func setupTableView() {
        super.setupTableView()
        //此方法无法高亮,使用下一种方法
        //navigationItem.leftBarButtonItem = UIBarButtonItem(title: "好友", style: .plain, target: self, action: #selector(showFriends))
        
        //swift调用OC但会instancetype的方法，判断不出是否可选,此方法在WBDemoViewController中也有使用，抽取出来放在Extensions中UIBarButtonItem+Extensions的扩展
//        let btn:UIButton = UIButton.cz_textButton("好友", fontSize: 16, normalColor: UIColor.darkGray, highlightedColor: UIColor.orange)
//        btn.addTarget(self, action: #selector(showFriends), for: .touchUpInside)
//        navigationItem.leftBarButtonItem = UIBarButtonItem(customView: btn)
        
        navItem.leftBarButtonItem = UIBarButtonItem(title: "好友", target: self, action: #selector(showFriends))
        
        //注册原型cell
        tableView?.register(UINib(nibName: "WBStatusNormalCell", bundle: nil), forCellReuseIdentifier: cellID)
        // 设置行高
        tableView?.rowHeight = UITableViewAutomaticDimension
        tableView?.estimatedRowHeight = 300 // 预估行高
        // 取消分割线
        tableView?.separatorStyle = .none
        
        setupNavTitle()
    }
    
    func setupNavTitle() {
        
        let title = WBNetWorkManager.shared.userAccount.screen_name
        
        let button = WBTitleButton(title: title)
        
        navItem.titleView = button
        
        button.addTarget(self, action: #selector(clickTitleButton(btn:)), for: .touchUpInside)
        
    }
    
    func clickTitleButton(btn: UIButton) {
        //设置选中状态
        btn.isSelected = !btn.isSelected
    }
    
}
